// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    steps:'',
    nowaddr:'点击显示当前地址'
  },
  onLoad:function(res){
    this.getLocation()
  },
  onChange(e) {
    this.setData({
      value: e.detail,
    });
  },
  onClick() {
    this.address2lnglat(this.data.value)
  },
  
  // 获取当前地址信息
  getLocation:function(e){
    var _this = this
    wx.getLocation({
      type: 'wgs84',
      success (res) {
        console.log(res)
        
        _this.setData({
          Now_address:res
        })
        
      }
    })
  },

  // 经纬度转地址
  lnglat2address:function(){
    var _this=this
    var lat = this.data.Now_address.latitude
    var lng = this.data.Now_address.longitude
    wx.request({
      url: 'https://api.cn/api/lnglat2address/',
      method:'GET',
      data:{lng:lng,lat:lat},
      success:function(res){
        console.log(res)
        _this.setData({nowaddr:res.data.result.address})
      }
    })
  },
// 获取目标地址经纬度
  address2lnglat:function(address){
    var _this = this
    wx.request({
      url: 'https://api.cn/api/address2lnglat',
      method:'GET',
      data:{address:address,format:'json'},
      success:function(res){
        console.log(res.data)
        _this.setData({
          Obj_address:res.data
        })
      }
    })
    this.navigation()
  },

  // 开始导航
  navigation:function(e){
    var _this = this
    var from = this.data.Now_address.latitude +','+this.data.Now_address.longitude
    var to = this.data.Obj_address.lat + ',' + this.data.Obj_address.lng
    // console.log(to)
    wx.request({
      url: 'https://api.cn/api/navigation',
      method:'GET',
      data:{from:from,to:to},
      success:function(res){
        console.log(res)
        console.log(res.data.result.routes[0].steps)
        _this.setData({steps:res.data.result.routes[0].steps,info:res.data.result.routes[0]})
      }
    })
    
  }
})
